package p.ithorns.sample;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.annotation.Resource;
import javax.servlet.MultipartConfigElement;

/**
 * ModuleAConfig
 *
 * @author HtL
 * @date 2024/7/18 16:14
 * @since 1.0.0
 */
@Configuration
public class ModuleAConfig implements ModulePathConfig {

    public static final String PATH = "/ma";

    private final static String MODULE_NAME = "ModuleA";

    @Resource
    private MultipartConfigElement multipartConfig;

    // 不需要单独定义了，统一在ServletRegistrationRegistrar中注入
    //  @Bean(MODULE_NAME)
    public ServletRegistrationBean<DispatcherServlet> registerServlet(WebApplicationContext context) {
        String path = PATH + "/*";
       // AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        DispatcherServlet servlet = new DispatcherServlet(context);
        ServletRegistrationBean<DispatcherServlet> bean = new ServletRegistrationBean<>(servlet, path);
        bean.setName(MODULE_NAME);
        if (null != multipartConfig) {
            bean.setMultipartConfig(multipartConfig);
        }
        return bean;
    }

    @Override
    public String getModuleName() {
        return MODULE_NAME;
    }

    @Override
    public String getPath() {
        return PATH + "/*";
    }
}